<template>
  <div class="tags" v-if="showTags">
    <ul>
      <li
        class="tags-li"
        v-for="(item,index) in tagsList"
        :class="{'active': isActive(item.path)}"
        :key="index"
      >
        <a class="tags-li-title" @click="goLink(item.path)">{{item.title}}</a>
        <span class="tags-li-icon" @click="closeTags(index)">
          <i class="el-icon-close"></i>
        </span>
      </li>
    </ul>
  </div>
</template>

<script>
import bus from "./bus";
export default {
  data() {
    return {
      tagsList: [],
    };
  },
  methods: {
    goLink(path) {
      this.$router.push({ path: path });
      if (path == "/manage") {
        bus.$emit("isIndex", true);
      } else {
        bus.$emit("isIndex", false);
      }
    },
    isActive(path) {
      return path === this.$route.fullPath;
    },
    // 关闭单个标签
    closeTags(index) {
      const delItem = this.tagsList.splice(index, 1)[0];
      const item = this.tagsList[index]
        ? this.tagsList[index]
        : this.tagsList[index - 1];
      if (item) {
        delItem.path === this.$route.fullPath && this.$router.push(item.path);
      } else {
        this.$router.push("/");
      }
    },
    // 关闭全部标签
    closeAll() {
      this.tagsList = [];
      this.$router.push("/");
    },
    // 关闭其他标签
    closeOther() {
      const curItem = this.tagsList.filter((item) => {
        return item.path === this.$route.fullPath;
      });
      this.tagsList = curItem;
    },
    // 设置标签
    setTags(route) {
      const isExist = this.tagsList.some((item) => {
        return item.path === route.fullPath;
      });
      if (!isExist) {
        if (this.tagsList.length >= 12) {
          this.tagsList.shift();
        }
        this.tagsList.push({
          title: route.meta.title,
          path: route.fullPath,
          name: route.matched[1].components.default.name,
        });
      }
      bus.$emit("tags", this.tagsList);
    },
    handleTags(command) {
      command === "other" ? this.closeOther() : this.closeAll();
    },
  },
  computed: {
    showTags() {
      return this.tagsList.length > 0;
    },
  },
  watch: {
    $route(newValue, oldValue) {
      this.setTags(newValue);
    },
  },
  created() {
    this.setTags(this.$route);

  },
};
</script>
<style scoped>
.tags-li-icon {
  position: relative;
  z-index: 99;
}
.tags {
  position: relative;
  height: 43px;
  overflow: hidden;
  background: #f5f6fb;
  border-bottom: 2px solid #e5e5e5;
  width: 100%;
  /*margin-left: 5px;*/
}

.tags ul {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  padding-top: 11px;
}

.tags-li {
  float: left;
  margin: 3px 3px 0px 0px;
  font-size: 14px;
  overflow: hidden;
  cursor: pointer;
  width: 106px;
  height: 28px;
  line-height: 28px;
  background: #e5e5e5;
  padding: 0 9px 0 12px;
  vertical-align: middle;
  color: #999999;
  display: flex;
  justify-content: space-between;
}

.tags-li:not(.active):hover {
  background: #ffff;
  color: #62717c;
}

.tags-li.active {
  color: #62717c;
  border-bottom: 2px solid #4466d8;
  background: #ffff;
}

.tags-li-title {
  float: left;
  max-width: 80px;
  width: 80px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #666;
}

.tags-li.active .tags-li-title {
  color: #62717c;
}
</style>
